package observerPattern.observerAPattern;

import java.util.ArrayList;

public class ShopSubject implements Subject {
    String goodsName;
    double oldPrice, newPrice;
    ArrayList<Observer> customerList;

    public ShopSubject() {
        customerList = new ArrayList<Observer>();
    }

    @Override
    public void addObserver(Observer o) {
        if(!(customerList.contains(o))){
            customerList.add(o);
        }
    }

    @Override
    public void deleteObserver(Observer o) {
        if(customerList.contains(o)){
            customerList.remove(o);
        }
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : customerList){
            observer.update();
        }
    }

    public void  setDiscountGoods(String name,double oldP, double newP){
        goodsName = name;
        oldPrice = oldP;
        newPrice = newP;
        notifyObservers();
    }

    public String getGoodsName() {
        return goodsName;
    }

    public double getOldPrice() {
        return oldPrice;
    }

    public double getNewPrice() {
        return newPrice;
    }
}

